package com.nit.mapper;

import com.nit.dto.GoodsDTO;
import com.nit.entity.GoodsEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface GoodsMapper {

    @Select("<script> " +
            "SELECT COUNT(*) " +
            "FROM tb_goods " +
            "WHERE is_delete = 0 " +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "<if test='goodsCate != null'> AND goods_cate = #{goodsCate} </if>" +
            "</script> ")
    Long count4Table(@Param("goodsName") Object goodsName,
                     @Param("goodsCate") Object goodsCate);

    @Select("<script> " +
            "SELECT " +
            "goods_id, goods_name, goods_cate, gmt_create " +
            "FROM tb_goods " +
            "WHERE is_delete = 0 " +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "<if test='goodsCate != null'> AND goods_cate = #{goodsCate} </if>" +
            "ORDER BY goods_id DESC " +
            "LIMIT #{start}, #{length} " +
            "</script> ")
    List<GoodsDTO> list4Table(@Param("start") Integer start,
                              @Param("length") Integer length,
                              @Param("goodsName") Object goodsName,
                              @Param("goodsCate") Object goodsCate);

    @Insert("INSERT INTO " +
            "tb_goods (" +
            "goods_name, goods_cate, " +
            "sells_in, sells_out, " +
            "is_delete, gmt_create) " +
            "VALUES (" +
            "#{goodsEntity.goodsName}, #{goodsEntity.goodsCate}, " +
            "#{goodsEntity.sellsIn}, #{goodsEntity.sellsOut}, " +
            "0, #{goodsEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "goodsId", keyColumn = "goods_id")
    Integer add(@Param("goodsEntity") GoodsEntity goodsEntity);

    @Update("UPDATE tb_goods " +
            "SET is_delete = 1 " +
            "WHERE goods_id = #{goodsId} ")
    Integer remove(@Param("goodsId") Long goodsId);

    @Update("UPDATE tb_goods " +
            "SET " +
            "goods_name = #{goodsDTO.goodsName}, goods_cate = #{goodsDTO.goodsCate}, " +
            "sells_in = #{goodsDTO.sellsIn}, sells_out = #{goodsDTO.sellsOut} " +
            "WHERE goods_id = #{goodsDTO.goodsId} ")
    Integer update(@Param("goodsDTO") GoodsDTO goodsDTO);
}
